﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Xml;

namespace COC.Commons
{
    public class CodingPlan
    {
        private readonly XmlDocument _ConfigXml = new XmlDocument();
        public List<PlanTask> PlanTasks;
        private XmlNode _SrcElement;
        private readonly PlanTaskManagerConfig _PlanTaskManagerConfig = new PlanTaskManagerConfig();

        public CodingPlan()
        {
            PlanTasks = new List<PlanTask>();
            Init();
        }

        public void Init()
        {
            //这里需要加上判断路径下是否有"CodingPlan.xml"文件，如果没有则程序复制一个模板过去。

            _ConfigXml.Load(Path.Combine(_PlanTaskManagerConfig.SyncPath, "CodingPlan.xml"));
            _SrcElement = _ConfigXml.DocumentElement?.SelectSingleNode("/CodingPlan");
            PlanTasks = GetPlanTasks();
        }

        /// <summary>
        /// 从CodingPlan.xml文件获取任务列表
        /// </summary>
        /// <returns></returns>
        private List<PlanTask> GetPlanTasks()
        {
            PlanTasks.Clear();
            if (_SrcElement != null)
            {
                foreach (XmlElement ele in _SrcElement.ChildNodes)
                {
                    var pt = new PlanTask()
                    {
                        Id = ele.GetAttribute("Id"),
                        Content = ele.GetAttribute("Content"),
                        Type = ele.GetAttribute("type"),
                        State = ele.GetAttribute("state"),
                        InsData = ele.GetAttribute("insDate"),
                        FulFillData = ele.GetAttribute("fulFillDate")
                    };
                    if (ele.ChildNodes.Count > 0)
                    {
                        foreach (XmlElement cele in ele.ChildNodes)
                        {
                            var tc = new TaskComment()
                            {
                                Id = cele.GetAttribute("Id"),
                                Time = cele.GetAttribute("Time"),
                                Content = cele.GetAttribute("Content")
                            };
                            pt.Comments.Add(tc);
                        }
                    }
                    PlanTasks.Add(pt);
                }
            }
            PlanTasks.Reverse();
            return PlanTasks;
        }

        /// <summary>
        /// 根据任务id获取任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PlanTask GetTaskFromId(List<PlanTask> pts,string id)
        {
            PlanTask task = new PlanTask();
            foreach (var pt in pts)
            {
                if (pt.Id.Equals(id))
                {
                    return pt;
                }
            }
            return task;
        }

        public void SaveNewComment(string id,TaskComment tc)
        {
            XmlNode xn = _SrcElement.SelectSingleNode("//CodingPlan//task[@Id = " + id + "]");
            XmlElement nc = _ConfigXml.CreateElement("comment");
            nc.SetAttribute("Id", tc.Id);
            nc.SetAttribute("Time", tc.Time);
            nc.SetAttribute("Content", tc.Content);
            xn.AppendChild(nc);
            _ConfigXml.Save(Path.Combine(_PlanTaskManagerConfig.SyncPath, "CodingPlan.xml"));
        }

        public void SaveNewPlanTask(PlanTask pt)
        {
            XmlNode xn = _SrcElement.SelectSingleNode("//CodingPlan");
            XmlElement nc = _ConfigXml.CreateElement("task");
            nc.SetAttribute("Id", pt.Id);
            nc.SetAttribute("Content",pt.Content);
            nc.SetAttribute("type",pt.Type);
            nc.SetAttribute("state", pt.State);
            nc.SetAttribute("insDate", pt.InsData);
            nc.SetAttribute("fulFillDate",pt.FulFillData);
            xn?.AppendChild(nc);
            _ConfigXml.Save(Path.Combine(_PlanTaskManagerConfig.SyncPath, "CodingPlan.xml"));
        }

        public void SaveEditPlanTask(PlanTask pt)
        {
            XmlElement nc = (XmlElement)_ConfigXml.SelectSingleNode("//CodingPlan//task[@Id = " + pt.Id + "]");
            if (nc != null)
            {
                nc.SetAttribute("type", pt.Type);
                nc.SetAttribute("state", pt.State);
                nc.SetAttribute("Content", pt.Content);
                nc.SetAttribute("insDate", pt.InsData);
                nc.SetAttribute("fulFillDate", pt.FulFillData);
            }
            _ConfigXml.Save(Path.Combine(_PlanTaskManagerConfig.SyncPath, "CodingPlan.xml"));
        }
    }
}
